package com.ruicar.afs.cloud.afscase.apply.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.apply.service.PrintManageService;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.PushDataForPos;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormFieldMapping;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormManage;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormSealMapping;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormFieldMappingService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormManageService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormSealMappingService;
import com.ruicar.afs.cloud.bizcommon.print.vo.TemplateDataSendVo;
import com.ruicar.afs.cloud.bizcommon.print.vo.TemplateRuleVo;
import com.ruicar.afs.cloud.bizcommon.rules.entity.*;
import com.ruicar.afs.cloud.bizcommon.rules.mapper.*;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleOperateHisService;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.enums.SynchronizationStateEnum;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: fangchenliang
 * @Date: 2020/8/6 11:23
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class PrintManageServiceImpl implements PrintManageService {

    private ComPrintFormFieldMappingService comPrintFormFieldMappingService;
    private PushDataForPos pushDataForPos;
    private AfsRuleInfoService ruleInfoService;
    private AfsRuleInputMappingMapper mappingMapper;
    private AfsRuleItemMapper ruleItemMapper;
    private AfsRuleOperateHisService ruleOperateHisService;
    private AfsRuleTransExpressMapper ruleTransExpressMapper;
    private AfsRuleExpressMapper ruleExpressMapper;
    private AfsRuleExpressAtomMapper ruleExpressAtomMapper;
    private AfsRuleResultMappingMapper ruleResultMappingMapper;
    private ComAttachmentFileService comAttachmentFileService;
    private ComPrintFormManageService comPrintFormManageService;
    private ComPrintFormSealMappingService comPrintFormSealMappingService;
    /**
     * @Description:
     * @Author: fangchenliang
     * @Date: 2020/7/21 20:32
     * @Param: [comPrintFormManage]
     * @Return: void
     */
    public void submitPrintFormManage(ComPrintFormManage comPrintFormManage) {
        /**发送至进件系统*/
        AfsTransEntity<TemplateDataSendVo> transEntity = new AfsTransEntity();
        transEntity.setTransCode(MqTransCode.AFS_POS_APPLY_CASE_CTM_NEW_TEMPLATE);
        /**发送给合同系统*/
        AfsTransEntity<TemplateDataSendVo> transEntityContract = new AfsTransEntity();
        transEntityContract.setTransCode(MqTransCode.AFS_POS_CASE_CONTRACT_CTM_NEW_TEMPLATE);

        TemplateDataSendVo dataSendVo = new TemplateDataSendVo();
        dataSendVo.setComPrintFormManage(comPrintFormManage);
        List<ComPrintFormFieldMapping> mappingList = comPrintFormFieldMappingService.list(Wrappers.<ComPrintFormFieldMapping>query().lambda().eq(ComPrintFormFieldMapping::getPrintFormId, comPrintFormManage.getPrintFormId()));
        dataSendVo.setFieldList(mappingList);
        List<TemplateRuleVo> ruleVoList = new ArrayList();
        /**List版本倒叙排列*/
        List<AfsRuleInfo> afsRuleInfoList = ruleInfoService.list(Wrappers.<AfsRuleInfo>query().lambda().eq(AfsRuleInfo::getRuleNo, String.valueOf(comPrintFormManage.getId())).orderByDesc(AfsRuleInfo::getRuleVersion));
        afsRuleInfoList.forEach(afsRuleInfo->{
            TemplateRuleVo ruleVo = new TemplateRuleVo();
            ruleVo.setRuleInfo(afsRuleInfo);
            List<AfsRuleExpress> ruleExpressList = ruleExpressMapper.selectList(Wrappers.<AfsRuleExpress>query().lambda().eq(AfsRuleExpress::getRuleId, afsRuleInfo.getId()));
            ruleVo.setRuleExpress(ruleExpressList);
            List<AfsRuleExpressAtom> ruleExpressAtomList = ruleExpressAtomMapper.selectList(Wrappers.<AfsRuleExpressAtom>query().lambda().eq(AfsRuleExpressAtom::getRuleId, afsRuleInfo.getId()));
            ruleVo.setRuleExpressAtom(ruleExpressAtomList);
            List<AfsRuleInputMapping> ruleInputMappingList = mappingMapper.selectList(Wrappers.<AfsRuleInputMapping>query().lambda().eq(AfsRuleInputMapping::getRuleId, afsRuleInfo.getId()));
            ruleVo.setRuleInputMapping(ruleInputMappingList);
            List<AfsRuleItem> ruleItemList = ruleItemMapper.selectList(Wrappers.<AfsRuleItem>query().lambda().eq(AfsRuleItem::getRuleId, afsRuleInfo.getId()));
            ruleVo.setRuleItem(ruleItemList);
            List<AfsRuleOperateHis> ruleOperateHisList = ruleOperateHisService.list(Wrappers.<AfsRuleOperateHis>query().lambda().eq(AfsRuleOperateHis::getTargetRuleId, afsRuleInfo.getId()));
            ruleVo.setRuleOperateHis(ruleOperateHisList);
            List<AfsRuleTransExpress> ruleTransExpressList = ruleTransExpressMapper.selectList(Wrappers.<AfsRuleTransExpress>query().lambda().eq(AfsRuleTransExpress::getRuleId, afsRuleInfo.getId()));
            ruleVo.setRuleTransExpress(ruleTransExpressList);
            List<AfsRuleResultMapping> resultMappingList = ruleResultMappingMapper.selectList(Wrappers.<AfsRuleResultMapping>query().lambda().eq(AfsRuleResultMapping::getRuleId, afsRuleInfo.getId()));
            ruleVo.setRuleResultMapping(resultMappingList);
            ruleVoList.add(ruleVo);
        });
        dataSendVo.setRuleVoList(ruleVoList);
        List<ComAttachmentFile> comAttachmentFileList = comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo, String.valueOf(comPrintFormManage.getId())).orderByDesc(ComAttachmentFile::getUploadTime));
        if(!CollectionUtils.isEmpty(comAttachmentFileList)){
            dataSendVo.setComAttachmentFile(comAttachmentFileList.get(0));
        }else{
            log.info("{}打印模板无word模板数据",comPrintFormManage.getPrintFormName());
        }
        List<ComPrintFormSealMapping> comPrintFormSealMappingList=comPrintFormSealMappingService.list(Wrappers.<ComPrintFormSealMapping>query().lambda()
                .eq(ComPrintFormSealMapping::getTemplateId, comPrintFormManage.getId()));
        if(!CollectionUtils.isEmpty(comPrintFormSealMappingList)){
            dataSendVo.setComPrintFormSealMappingList(comPrintFormSealMappingList);
        }else{
            log.info("{}打印模板无签章签字模板数据",comPrintFormManage.getPrintFormName());
        }
        transEntity.setData(dataSendVo);
        pushDataForPos.sendTemplateInfo(transEntity);
        log.info("{}打印模板发送进件系统成功！",comPrintFormManage.getPrintFormName());
        transEntityContract.setData(dataSendVo);
        /**暂时不启用同步到合同系统*/
        pushDataForPos.sendTemplateToContract(transEntityContract);
        log.info("{}打印模板发送合同系统成功！",comPrintFormManage.getPrintFormName());
        comPrintFormManage.setSynchronizationState(SynchronizationStateEnum.SEND.getCode());
        comPrintFormManageService.updateById(comPrintFormManage);

    }
}
